package cn.ljy.common.utils;

import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

public class MyObjectUtils {
    /**
     * 判断类中每个属性是否都为空
     *
     * @param object
     * @return
     */
    public static boolean checkObjAllFieldsIsNull(Object object) {
        boolean result = true;//默认为空
        if (null == object) {
            result = true;
        } else {
            try {
                // 先遍历该类，getDeclaredFields不能遍历父类的属性。所以要遍历到父类
                Class currentClass = object.getClass();// 获取当前类的class
                while (!currentClass.equals("object")) {//只要没有到最顶级的object父类
                    for (Field f : object.getClass().getDeclaredFields()) { // getDeclaredFields获取该类的所有属性
                        f.setAccessible(true);
                        System.out.println(f.get(object));
                        System.out.println(f.getType());
                        if (f.get(object) != null && StringUtils.hasLength(f.get(object).toString())) {
                            result = false;
                            break;
                        }
                    }
                    //让当前类变为父类
                    currentClass = currentClass.getSuperclass();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

}